/*
* @Author: admin
* @Date:   2017-07-28 09:22:53
* @Last Modified by:   admin
* @Last Modified time: 2017-08-01 09:05:48
*/
$(document).ready(function () {
    /*
    IOSWebview方法调用
     */
    function setupWebViewJavascriptBridge(callback) {
      if (window.WebViewJavascriptBridge) {
          return callback(WebViewJavascriptBridge); 
      }
     if (window.WVJBCallbacks) { 
          return window.WVJBCallbacks.push(callback); 
      }

         window.WVJBCallbacks = [callback];
         var WVJBIframe = document.createElement('iframe');
         WVJBIframe.style.display = 'none';
         WVJBIframe.src = 'https://__bridge_loaded__';
         document.documentElement.appendChild(WVJBIframe);

     setTimeout(function() {
         document.documentElement.removeChild(WVJBIframe) }, 0);
      }
     setupWebViewJavascriptBridge(
        function(bridge) {}
     ); 
    //安卓和IOS的跳转逻辑不同，应将所有的a标签的href属性替换为正确的链接
    var browser = {
        versions: function () {
            var u = navigator.userAgent,
                app = navigator.appVersion;
            return { //移动终端浏览器版本信息
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile/i) || !!u.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
            };
        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };

    var getUrlParam = function(name){
                var reg     = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
                var result  = window.location.search.substr(1).match(reg);
                return result ? decodeURIComponent(result[2]) : '';
                };
                // 获取到userid和order_number
    var getuserid = getUrlParam('userid');
   /**
   * 根据系统浏览器的不同更换URL地址
   * @method changeURL
   * @param  {[string]}  params [字符串参数，基本形式为'0,0,0,0,0'，分别代表goods,classify3,activity,model,brand]
   * @return {[string]}        [修改后的href地址]
   */
        function changeURL(params) {
            //将'0,0,0,0,0'以逗号分隔为数组[0,0,0,0,0]
            var property = params.split(',');
            var href = "javascript:demo.startRemove('{goods:" + property[0] + ",classify3:" + property[1] + ",activity:" + property[2] + ",model:" + property[3] + ",brand:" + property[4] + "}')";
            //IOS系统
            if (browser.versions.iPhone || browser.versions.iPad || browser.versions.ios) {
                href = "javascript:WebViewJavascriptBridge.callHandler('ObjC Echo',{goods:" + property[0] + ",classify3:" + property[1] + ",activity:" + property[2] + ",model:" + property[3] + ",brand:" + property[4] + "})";
            }
            return href;
        }
        //遍历所有的a元素，将其data-params的值读取出来，并根据changeURL返回的值设置为其href属性
        $.each($('a'), function (index, value) {
            var params = $(this).attr('data-params').replace(/\s/g, '');
            var myHref = changeURL(params);
             $(this).attr('href', myHref);
        });
        var timeObj = new Date(),
        month = timeObj.getMonth() + 1,
        date = timeObj.getDate(),
        today = (month < 10 ? '0' + month : month).toString() + '-' + (date < 10 ? '0' + (date) : date).toString();
        today = '08-01';
        var todayTime = new Date(today).getTime();
        // 每日惊喜价参数
        var goods = {
        '08-01_08-02': 7173,
        '08-03_08-04': 6862,
        '08-05_08-06': 4461,
        '08-07_08-08': 13162,
        '08-09_08-10': 24085,
        };
        /**
          * 修改每日惊喜价group下的a标签href属性以及子元素img的src属性
          * @method changeDaily
          * @return {[null]}    [无]
        */
        function changeDaily() {
        var keys = Object.keys(goods);
        keys.forEach(function (key, index) {
            var timeArr = key.split('_'),
                startTime = new Date(timeArr[0]).getTime(),
                endTime = new Date(timeArr[1]).getTime();

                if (todayTime >= startTime && todayTime <= endTime) {
                    console.log(key);
                    var goodsNext = goods[keys[index + 1]];
                $('.evwerySurprise-content a').eq(0).attr('href', changeURL(goods[key] + ',0,0,0,0')).children('img').attr('src', 'image/everydaySurprise/' + key + '/01.jpg');
                $('.evwerySurprise-content a').eq(1).attr('href', changeURL(goodsNext + ',0,0,0,0')).children('img').attr('src', 'image/everydaySurprise/' + key + '/02.jpg');
                }
            });
        }
        changeDaily();
        var newHref = 'http://m.jkl.com.cn/lumpwindow/award/index.html?userid='+ getuserid;
        console.log(newHref);
        $('#award').attr('href',newHref);



})